<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
$return=array();
if($_RQ['op']=='get_detail'){
	$shopset = $this->plugin_setting('shopset');
	set_html('title','商品详情');
	$id=intval($_RQ['id']);
	if($id){
		$_SESSION['goodsid'] = $id;
	}
	if(!$id){
		exi('没有商品ID','error','index/');
	}
	$goods=pdo_get('jy_supplier_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$goods['images']=json_decode($goods['images'],true);
	
	if(!$goods){
		exi('选择的商品不存在，请重新选择！','error','index/');
	}
	pdo_update('jy_supplier_goods',array('readnum +='=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['merch']=pdo_get('jy_supplier_user',array('id'=>$goods['sid']),array('merchname','logo','desc','id'));

	
  

	$return['address_list'] = pdo_getall('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($return['address_list']){
		foreach($return['address_list'] as &$add){
			$add['pcd']=json_decode($add['pcd'],true);
		}
	}
	//会员默认地址
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	//收货地址：
	$address['pcd']=json_decode($address['pcd'],true);
	//$seleaddress = $address['pcd']['province'].$address['pcd']['city'].$address['pcd']['district'].$address['address'];
	$return['address'] = $address;
	//预计送达时间
	$deliverytime = date("m月d日",strtotime("+2 day"));
	$return['deliverytime'] = $deliverytime;
	//是否可添加购物车
	$canAddCart = true;



	//热销商品（按笔数）
	$params=array(
			'table'=>'jy_supplier_goods',
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),
			'fields'=>array('id','title','image','price','ispresell','total','salenum'),
			'orderby'=>'salenum desc',
			'limit'=>'6',
	);
	$hotList=array();//pdo_getlist($params);
	$return['hotList']=$hotList;
	//商品描述为空
	$goods['content']=$goods['content']?$goods['content']:'<div></div>';
	//参数
	$goods['param']=json_decode($goods['param'],true);
	//问答
	$wenda = array();
	$return['wenda']=$wenda;
	//货物地址：
	$goods['pcd'] = json_decode($goods['pcd'],true);
	//POST提交：
	$post = array();
	$post['num'] = 1;
	$post['goodsid'] = $id;
	$post['specid']=$selectspecs;
	$post['addressid']=$address['id'];
	$return['post']=$post;
	$return['goods']=$goods;
	$return['canAddCart']=$canAddCart;
	$return['cartCount']=$cartCount;
	$return['coupons']=$coupons;
	$return['gifts']=$gifts;
	$return['isgift']=$isgift;
	$return['has_city']=$has_city;
	$return['citys']=$citys;
	$return['dispatch']=$dispatch;
	$return['spec_titles']=$spec_titles;
	$return['shopdetail']=$shopdetail;
	$return['sale_enough']=$sale_enough;
	$return['sale_baoyou']=$sale_baoyou;
	$return['fullbackgoods']=$fullbackgoods;
	$return['images']=$goods['images'];
	$return['showtype']=0;
	//现在时间时间戳
	$return['time']=time();
	$date_time=date('Y-m-d H:i:s',$return['time']);
	$hasServices = $goods['cash'] || $goods['seven'] || $goods['repair'] || $goods['invoice'] || $goods['quality'];
	$return['hasServices']=$hasServices;
	$return['goodsurl']=custom_url('jy_pcweb/goods.detail//id='.$id,true);
	set_html('title',$goods['title']);
	$return['sharedata']=array(
		'title'=>$goods['title'],
		'desc'=>$goods['subtitle']?$goods['subtitle']:$goods['title'],
		'link'=>custom_url('jy_supplier/goods_detail//id='.$id.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$goods['image']
	);
	//获取活动信息
	if($goods['active_plugin']){
		$return['item']['active']=$this->model('activity')->get_active($goods['id'],$goods['active_id'],$goods['active_plugin']);
		if($return['item']['active']){
			$return['item']['price']=$return['item']['active']['price']?:$return['item']['price'];
			$return['item']['active_url']=$this->model('activity')->active_url($goods['id'],$goods['active_id'],$goods['active_plugin']);
		}
	}
	$return['item']['price']=price($return['item']['price']);
	exi($return);
}

if($_RQ['op']=='addtocart'){
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	if(!$id){
		exi('选择的商品不存在，请重新选择！','error');
	}
	if(!$number){
		$number=1;
	}
	$spsids = '';
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
	}
	$optionid = pdo_getcolumn('jy_supplier_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	$goods=pdo_get('jy_supplier_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$ifgoods=pdo_get('jy_supplier_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$goods['price'],
		'optionid'=>$optionid,
	);
	if($ifgoods){
		$data['total']=$ifgoods['total']+$number;
		pdo_update('jy_supplier_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
	}
	else{
		$data['total']=$number;
		pdo_insert('jy_supplier_member_cart',$data);
	}
	exi('','','/member.cart/');
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}

	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_supplier_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$return['options'] = pdo_get('jy_supplier_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']));
	//默认商品规格价格：
	if($return['options']){
		$goods['price']=$return['options']['price'];
	}
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='change_address'){
	$post=get_postdata();
	$addressid = intval($post['addressid']);
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address'] =$address;
	exi($return);
}
